package com.bstek.urule.console.servlet.ul;

import com.bstek.urule.RuleException;
import com.bstek.urule.Utils;
import com.bstek.urule.builder.ResourceLibraryBuilder;
import com.bstek.urule.console.servlet.base.BaseController;
import com.bstek.urule.console.repository.RepositoryService;
import com.bstek.urule.dsl.DSLRuleSetBuilder;
import com.bstek.urule.model.library.ResourceLibrary;
import com.bstek.urule.model.rule.RuleSet;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * <p>
 *
 * </p>
 *
 * @author zhouchaoliang
 * @since 2021/12/9
 */
@Controller
public class ULEditorController extends BaseController {
    @Autowired
    private DSLRuleSetBuilder dslRuleSetBuilder;
    @Autowired
    private ResourceLibraryBuilder resourceLibraryBuilder;
    @Autowired
    private RepositoryService repositoryService;

    @GetMapping("/urule/uleditor")
    public ModelAndView uleditor(HttpServletRequest req, HttpServletResponse resp) {
        ModelAndView mv = new ModelAndView("/ul-editor");
        mv.addObject("contextPath", req.getContextPath());
        String file = req.getParameter("file");
        file = Utils.decodeURL(file);
        String project = buildProjectNameFromFile(file);
        if (project != null) {
            mv.addObject("project", project);
        }
        return mv;
    }

    @PostMapping("/urule/uleditor/loadUL")
    public void loadUL(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String file = req.getParameter("file");
        file = Utils.decodeURL(file);
        OutputStream outputStream = resp.getOutputStream();
        String version = req.getParameter("version");
        InputStream inputStream = null;
        try {
            if (StringUtils.isEmpty(version)) {
                inputStream = repositoryService.readFile(file, null);
            } else {
                inputStream = repositoryService.readFile(file, version);
            }
            IOUtils.copy(inputStream, outputStream);
        } catch (Exception ex) {
            throw new RuleException(ex);
        } finally {
            outputStream.close();
            inputStream.close();
        }
    }

    @ResponseBody
    @PostMapping("/urule/uleditor/loadULLibs")
    public ResourceLibrary loadULLibs(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String content = req.getParameter("content");
        RuleSet ruleSet = dslRuleSetBuilder.build(content);
        return resourceLibraryBuilder.buildResourceLibrary(ruleSet.getLibraries());
    }
}
